﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jumpton.Data;
using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using BusinessRule;

namespace Jumpton.SFA.Server.BusinessRule.System
{
    public class Assign_Permissions : BusinessLogic
    {
        #region 用户组

        private decimal _GROUP_ID;
        public decimal GROUP_ID
        {
            set { _GROUP_ID = value; }
            get { return _GROUP_ID; }
        }

        private decimal _Domain_id;
        public decimal Domain_id
        {
            set { _Domain_id = value; }
            get { return _Domain_id; }
        }
        #region 是否存在
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool IsExists(string code)
        {
            string sql = string.Format(@"SELECT COUNT(ID) FROM SYS_GROUPS WHERE CODE = '{0}' AND ACTIVE = 1", code);
            object obj = SqlData.ExecuteScalar(sql);
            return (int)obj > 0;
        }
        #endregion

        /// <summary>
        /// 得到用户组数据
        /// </summary>
        /// <returns></returns>
        public DataSet GetGroupData()
        {
            string sql = string.Format(@"SELECT ID,CODE c,DATA_PERMISSION p FROM SYS_GROUPS WHERE [DOMAIN_ID] = {0} AND ACTIVE = 1", Domain_id);
            DataSet ds = SqlData.OpenSql(sql);
            ds.Tables[0].TableName = "rows";

            return ds;
        }

        /// <summary>
        /// 得到用户组数据的Json字符串
        /// </summary>
        /// <returns></returns>
        public string GetGroupDataString()
        {
            return GetJsonString(GetGroupData());
        }

        public SYS_GROUPS LoadRow(int ID)
        {
            return (SYS_GROUPS)ObjectRow.Load(typeof(SYS_GROUPS), string.Format("this.ID='{0}'", ID));
        }

        /// <summary>
        /// 得到单条用户组数据
        /// </summary>
        /// <returns></returns>
        public DataSet GetGroupRowData()
        {
            string sql = string.Format(@"SELECT ID,CODE c,DATA_PERMISSION p FROM SYS_GROUPS WHERE ID = {0} AND ACTIVE = 1", GROUP_ID);
            DataSet ds = SqlData.OpenSql(sql);
            ds.Tables[0].TableName = "rows";

            return ds;
        }

        /// <summary>
        /// 得到单条用户组数据的Json字符串
        /// </summary>
        /// <returns></returns>
        public string GetGroupRowDataString()
        {
            return GetJsonString(GetGroupRowData());
        }

        /// <summary>
        /// 插入,更新,删除操作 用户组数据
        /// </summary>
        /// <returns></returns>
        public string Group_Save(SYS_GROUPS entity)
        {
            try
            {
                if (entity != null)
                {
                    entity.Save();
                }
                return "1";
            }
            catch (Exception err)
            {
                return "0";
            }
        }
        #endregion

        /// <summary>
        /// 返回Json字符串
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public string GetJsonString(DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return JsonHelper.DataSetToJson(ds);
            }
            else
                return "";
        }

        private DataSet ds = null;

        public DataSet GetTableData()
        {
            string sql = string.Format(@"SELECT M.ID A,M.NAME AA,MI.ID B,MI.NAME BB,SGD.MODULE_ITEMS_ID M,SGD.OPTION_ROLE O 
FROM MODULE_ITEMS MI LEFT JOIN MODULES M ON MI.MODULE_ID = M.ID 
LEFT JOIN SYS_GROUPS_DETAIL SGD ON SGD.MODULE_ITEMS_ID = MI.ID AND SGD.DOMAIN_ID = {0} AND SGD.GROUP_ID = {1}", Domain_id, GROUP_ID);
            return SqlData.OpenSql(sql);
        }

        public string GetTreeJsonString()
        {
            ds = GetTableData();
            string TempModule = string.Empty;
            string Module = string.Empty;
            string TempModule1 = string.Empty;
            string Module1 = string.Empty;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                int i = 1000;
                DataView dv = ds.Tables[0].DefaultView;
                DataTable dtDistinct = dv.ToTable(true, "A", "AA");
                foreach (DataRow dr in dtDistinct.Rows)
                {
                    Module += (@",{'id': " + i.ToString() + ", 'text': '" + dr["AA"].ToString() + "','state':'closed', 'children': [");
                    TempModule = "";

                    Module1 += (@",{'id': " + i.ToString() + ", 'text': '" + dr["AA"].ToString() + "','state':'closed', 'children': [");
                    TempModule1 = "";

                    foreach (DataRow drChild in ds.Tables[0].Select("A = " + dr["A"].ToString()))
                    {
                        TempModule += ",{";
                        TempModule += string.Format(@"'id': {0}, 'text': '{1}','checked':{2}", drChild["B"], drChild["BB"], string.IsNullOrEmpty(drChild["M"].ToString()) ? "false" : "true");
                        TempModule += "}";

                        TempModule1 += ",{";
                        TempModule1 += string.Format(@"'id': {0}, 'text': '{1}','checked':{2}", drChild["B"], drChild["BB"], (drChild["O"].ToString() == "1") ? "true" : "false");
                        TempModule1 += "}";
                    }
                    if (!string.IsNullOrEmpty(TempModule))
                        Module += (TempModule.Remove(0, 1));

                    if (!string.IsNullOrEmpty(TempModule1))
                        Module1 += (TempModule1.Remove(0, 1));

                    Module += ("]}"); Module1 += ("]}");
                    i++;
                }

                return "[" + Module.Remove(0, 1) + "]|[" + Module1.Remove(0, 1) + "]";
            }
            else
                return "";
        }

        public int SaveGroupDetail(int g, decimal d, string l, string m, string o)
        {
            return SqlData.ExecuteSP("SP_SaveGroupDetail", g, d, l, m, o);
        }
    }
}
